package com.hopedove.workshop.ui

import android.app.DatePickerDialog
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.LinearLayoutManager
import com.hopedove.base.BaseActivity
import com.hopedove.data.Datetime
import com.hopedove.workshop.R
import com.hopedove.workshop.data.pojo.ObservableRunningSituation
import com.hopedove.workshop.databinding.WorkshopRunningSituationQueryBinding
import com.hopedove.workshop.ui.adapter.RunningSituationQueryAdapter
import com.hopedove.workshop.viewmodel.WorkshopRunningSituationQueryViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 *  运转情况查询界面
 */
class RunningSituationQueryActivity : BaseActivity() {
    private val self = this

    private lateinit var binding: WorkshopRunningSituationQueryBinding
    private val viewModel: WorkshopRunningSituationQueryViewModel by viewModel()

    private var datePickerDialog: DatePickerDialog? = null

    private val mAdapter = RunningSituationQueryAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.workshop_running_situation_query)
        with(binding) {
            lifecycleOwner = this@RunningSituationQueryActivity
            vm = viewModel

            binding.rlList.layoutManager =
                LinearLayoutManager(self, LinearLayoutManager.VERTICAL, false)
            binding.rlList.adapter = mAdapter
        }

        setupViewModelObservers()
        viewModel.load()
    }

    private fun setupViewModelObservers() {
        observeStartPickDate(viewModel.pickStartDate)
        observeEndPickDate(viewModel.pickEndDate)
        observeSituationList(viewModel.observableSituations)
        observeToastText(viewModel.toastText)
    }

    private fun observeSituationList(list: LiveData<List<ObservableRunningSituation>>) {
        list.observe(this) {
            mAdapter.setData(it)
        }

    }

    private fun observeStartPickDate(event: LiveData<Datetime>) {
        event.observe(this, { date ->
            datePickerDialog?.dismiss()
            datePickerDialog = DatePickerDialog(
                this,
                datePickerListener,
                date.year(),
                date.month() - 1,
                date.dayOfMonth()
            )
            datePickerDialog?.updateDate(date.year(), date.month() - 1, date.dayOfMonth())
            datePickerDialog?.show()
        })
    }

    private fun observeEndPickDate(event: LiveData<Datetime>) {
        event.observe(this) { date ->
            datePickerDialog?.dismiss()
            datePickerDialog = DatePickerDialog(
                this,
                endDatePickListener,
                date.year(),
                date.month() - 1,
                date.dayOfMonth()
            )
            datePickerDialog?.updateDate(date.year(), date.month() - 1, date.dayOfMonth())
            datePickerDialog?.show()
        }
    }

    private val datePickerListener =
        DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
            viewModel.updateStartDate(Datetime(year, month + 1, dayOfMonth))
        }

    private val endDatePickListener =
        DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
            viewModel.updateEndDate(Datetime(year, month + 1, dayOfMonth))
        }

}